package com.ctrl.riveradministrator.mvp.view.activity.work_bench;


import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Message;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseFragment;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.mvp.view.activity.bulletin.BulletinActivity;
import com.ctrl.riveradministrator.mvp.view.activity.channel_segment.ChannelSegActivity;
import com.ctrl.riveradministrator.mvp.view.activity.contact.ContactActivity;
import com.ctrl.riveradministrator.mvp.view.activity.file_information.FileInfoActivity;
import com.ctrl.riveradministrator.mvp.view.activity.information.InformationActivity;
import com.ctrl.riveradministrator.mvp.view.activity.learn_zoom.LearnZoomActivity;
import com.ctrl.riveradministrator.mvp.view.activity.news.NewsActivity;
import com.ctrl.riveradministrator.mvp.view.activity.problem_list.ProblemListActivity;
import com.ctrl.riveradministrator.mvp.view.activity.problem_list.ProblemListView;
import com.ctrl.riveradministrator.mvp.view.activity.river_info.RiverInfoActivity;
import com.ctrl.riveradministrator.mvp.view.activity.scenery_share.SceneryShareActivity;
import com.ctrl.riveradministrator.mvp.view.activity.suggest.SuggestActivity;
import com.ctrl.riveradministrator.mvp.view.activity.to_do_list.ToDoActivity;
import com.ctrl.riveradministrator.mvp.view.activity.trace.TraceActivity;
import com.ctrl.riveradministrator.mvp.view.activity.trace.search.TraceSearchActivity;
import com.ctrl.riveradministrator.mvp.view.activity.trace.statistics.StatisticsActivity;
import com.ctrl.riveradministrator.mvp.view.activity.work_dynamic.WorkDynActivity;
import com.ctrl.riveradministrator.mvp.view.weight.GridSpaceItemDecoration;

/**
 * A simple {@link Fragment} subclass.
 */
public class WorkBenchFragment
        extends BaseFragment
        implements GridAdapter.GridClickListener {
    //"河道巡查", "待办事项",
    private String[] titles = {"河道巡查", "待办事项", "工作动态", "公示牌", "河流信息",
            "河段信息", "通讯录", "建言献策", "巡查记录", "通知公告", "文件通知", "美景分享",
            "学习园地", "问题处理", "巡河统计"};

    private int[] colors = {R.color.color_9, R.color.color_10, R.color.color_3, R.color.color_4,
            R.color.color_5, R.color.color_6, R.color.color_7, R.color.color_8, R.color.color_1, R.color.color_2,
            R.color.color_11, R.color.color_12, R.color.color_13, R.color.color_2,
            R.color.color_3};
    private int[] icons = {R.drawable.ic_hdxc, R.drawable.ic_dbsx, R.drawable.ic_gzdt,
            R.drawable.ic_gsp, R.drawable.ic_hlxx, R.drawable.ic_hdxx, R.drawable.ic_txl,
            R.drawable.ic_jyxc, R.drawable.ic_xcjl, R.drawable.ic_tzgg, R.drawable.ic_wjtz,
            R.drawable.ic_mjfx, R.drawable.ic_xxyd, R.drawable.ic_gzdt, R.drawable.ic_hlxx};
    private int[] iconsBack = {R.drawable.ic_bg10, R.drawable.ic_bg11, R.drawable.ic_bg3,
            R.drawable.ic_bg4, R.drawable.ic_bg5, R.drawable.ic_bg6, R.drawable.ic_bg7,
            R.drawable.ic_bg8, R.drawable.ic_bg9, R.drawable.ic_bg1, R.drawable.ic_bg2,
            R.drawable.ic_bg12, R.drawable.ic_bg13, R.drawable.ic_bg2, R.drawable.ic_bg3};
    private GridAdapter gridAdapter;
    private RecyclerView list;
    private LocationManager lm;

    public WorkBenchFragment() {
        // Required empty public constructor
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_work_bench;
    }

    @Override
    protected void initView(View view) {
        list = (RecyclerView) $(view, R.id.list);
        list.setLayoutManager(new GridLayoutManager(getContext(), 3));
        //list.addItemDecoration(new GridSpaceItemDecoration(40, 280, 20, 20, 20, 3));
        gridAdapter = new GridAdapter(getContext(), titles, icons, iconsBack, colors);
        list.setAdapter(gridAdapter);
    }

    @Override
    protected void doBusiness(Context mContext, Activity activity) {
        lm = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    }

    @Override
    protected void widgetClick(View view) {

    }

    @Override
    protected void setListener() {
        gridAdapter.setClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onGridItemClick(int position) {
        switch (position) {
            case 0:
                boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                if (ok) {
                    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                        Toast.makeText(getActivity(), "请在设置中打开位置权限", Toast.LENGTH_SHORT).show();
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0x01);
                    } else {
                        startActivity(new Intent(getActivity(), TraceActivity.class).setAction("task"));
                    }
                } else {
                    Toast.makeText(getActivity(), "系统检测到未开启GPS定位服务", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(intent, 1315);
                }
                break;
            case 1:
                startActivity(new Intent(getActivity(), ToDoActivity.class));
                break;
            case 2:
                startActivity(new Intent(getActivity(), WorkDynActivity.class));
                break;
            case 3:
                startActivity(new Intent(getActivity(), BulletinActivity.class));
                break;
            case 4:
                startActivity(new Intent(getActivity(), RiverInfoActivity.class));
                break;
            case 5:
                startActivity(new Intent(getActivity(), ChannelSegActivity.class));
                break;
            case 6:
                startActivity(new Intent(getActivity(), ContactActivity.class));
                break;
            case 7:
                startActivity(new Intent(getActivity(), SuggestActivity.class));
                break;
            case 8:
                startActivity(new Intent(getActivity(), TraceSearchActivity.class));
                break;
            case 9:
                startActivity(new Intent(getActivity(), InformationActivity.class));
                break;
            case 10:
                startActivity(new Intent(getActivity(), FileInfoActivity.class));
                break;
            case 11:
                startActivity(new Intent(getActivity(), SceneryShareActivity.class));
                break;
            case 12:
                startActivity(new Intent(getActivity(), LearnZoomActivity.class));
                break;
            case 13:
                startActivity(new Intent(getActivity(), ProblemListActivity.class));
                break;
            case 14:
                startActivity(new Intent(getActivity(), StatisticsActivity.class));
                break;
            default:
                Toast.makeText(getContext(), "p:" + position, Toast.LENGTH_LONG).show();
                break;
        }
    }

}
